package com.health.db.service.order.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.health.db.dao.order.IRegisterScheduleDao;
import com.health.db.dao.user.IUserInfoDao;
import com.health.db.dao.user.IUserPatientDao;
import com.health.db.model.RegisterSchedule;
import com.health.db.model.RegisterScheduleExample;
import com.health.db.model.UserInfo;
import com.health.db.model.UserPatient;
import com.health.db.service.order.IRegisterScheduleService;

@Component("registerScheduleService")
public class RegisterScheduleServiceImpl implements IRegisterScheduleService {
	
	@Autowired
	private IRegisterScheduleDao registerScheduleDao;
	
	
	@Autowired
	private IUserInfoDao userInfoDao;
	
	@Autowired
	IUserPatientDao userPatientDao;
	
	@Override
	public int insert(RegisterSchedule record) {
		// TODO Auto-generated method stub
		int executeNum= registerScheduleDao.insert(record);
		//修改默认
		if (null!=record.getUserId()) {
			
			
			UserInfo userInfo = userInfoDao.selectByPrimaryKey(record.getUserId());
			
			if (null!=userInfo&&(null==userInfo.getDefalutPatientId()||
					userInfo.getDefalutPatientId().longValue()!=record.getPatientId().longValue())) {
				
				UserInfo userInfoUpdate=new UserInfo();
				userInfoUpdate.setUserId(record.getUserId());
				userInfoUpdate.setDefalutPatientId(record.getPatientId());
				userInfoDao.updateByPrimaryKeySelective(userInfoUpdate);
				
				UserPatient userPatient =new UserPatient();
				userPatient.setUserId(record.getUserId());
				userPatient.setPatientId(record.getPatientId());
				userPatientDao.updateByUserIdPatientId(userPatient);
			}
			
		}
		return executeNum;
	}

	@Override
	public int updateByPrimaryKeySelective(RegisterSchedule record) {
		// TODO Auto-generated method stub
		return registerScheduleDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(RegisterSchedule record) {
		// TODO Auto-generated method stub
		return registerScheduleDao.updateByPrimaryKey(record);
	}

	@Override
	public List<RegisterSchedule> selectByPatientId(Long patientId) {
		// TODO Auto-generated method stub
		return registerScheduleDao.selectByPatientId(patientId);
	}

	public IRegisterScheduleDao getRegisterScheduleDao() {
		return registerScheduleDao;
	}

	public void setRegisterScheduleDao(IRegisterScheduleDao registerScheduleDao) {
		this.registerScheduleDao = registerScheduleDao;
	}

	@Override
	public List<RegisterSchedule> selectByExample(
			RegisterScheduleExample example) {
		// TODO Auto-generated method stub
		return registerScheduleDao.selectByExample(example);
	}

	@Override
	public RegisterSchedule selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return registerScheduleDao.selectByPrimaryKey(id);
	}

	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	
	
	

}
